48 #include "sensor_drv.h" 49 #include "sensor_io_i2c.h" 52 #define MMA845x_COUNTSPERG 8192.0 53 #define MMA8451_ACCEL_FIFO_SIZE 32 58 const registerreadlist_t MMA845x_WHO_AM_I_READ[] =
60 { .readFrom = MMA845x_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
64 const registerreadlist_t MMA845x_F_STATUS_READ[] =
66 { .readFrom = MMA845x_STATUS, .numBytes = 1 }, __END_READ_DATA__
70 registerreadlist_t MMA845x_DATA_READ[] =
72 { .readFrom = MMA845x_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
76 const registerwritelist_t MMA8451_Initialization[] =
81 { MMA845x_CTRL_REG1, 0x00, 0x00 },
87 { MMA845x_F_SETUP, 0x40, 0x00 },
94 { MMA845x_XYZ_DATA_CFG, 0x01, 0x00 },
103 { MMA845x_CTRL_REG2, 0x02, 0x00 },
118 #if (ACCEL_ODR_HZ <= 1) 119 { MMA845x_CTRL_REG1, 0x39, 0x00 },
120 #elif (ACCEL_ODR_HZ <= 6) 121 { MMA845x_CTRL_REG1, 0x31, 0x00 },
122 #elif (ACCEL_ODR_HZ <= 12) 123 { MMA845x_CTRL_REG1, 0x29, 0x00 },
124 #elif (ACCEL_ODR_HZ <= 50) 125 { MMA845x_CTRL_REG1, 0x21, 0x00 },
126 #elif (ACCEL_ODR_HZ <= 100) 127 { MMA845x_CTRL_REG1, 0x19, 0x00 },
128 #elif (ACCEL_ODR_HZ <= 200) 129 { MMA845x_CTRL_REG1, 0x11, 0x00 },
130 #elif (ACCEL_ODR_HZ <= 400) 131 { MMA845x_CTRL_REG1, 0x09, 0x00 },
133 { MMA845x_CTRL_REG1, 0x01, 0x00 },
140 const registerwritelist_t MMA845x_Initialization[] =
142 { MMA845x_CTRL_REG1, 0x00, 0x00 },
143 { MMA845x_XYZ_DATA_CFG, 0x01, 0x00 },
144 { MMA845x_CTRL_REG2, 0x02, 0x00 },
145 #if (ACCEL_ODR_HZ <= 1) 146 { MMA845x_CTRL_REG1, 0x39, 0x00 },
147 #elif (ACCEL_ODR_HZ <= 6) 148 { MMA845x_CTRL_REG1, 0x31, 0x00 },
149 #elif (ACCEL_ODR_HZ <= 12) 150 { MMA845x_CTRL_REG1, 0x29, 0x00 },
151 #elif (ACCEL_ODR_HZ <= 50) 152 { MMA845x_CTRL_REG1, 0x21, 0x00 },
153 #elif (ACCEL_ODR_HZ <= 100) 154 { MMA845x_CTRL_REG1, 0x19, 0x00 },
155 #elif (ACCEL_ODR_HZ <= 200) 156 { MMA845x_CTRL_REG1, 0x11, 0x00 },
157 #elif (ACCEL_ODR_HZ <= 400) 158 { MMA845x_CTRL_REG1, 0x09, 0x00 },
160 { MMA845x_CTRL_REG1, 0x01, 0x00 },
174 status = Register_I2C_Read(sensor->
bus_driver, sensor->
addr, MMA845x_WHO_AM_I, 1, ®);
175 if (status==SENSOR_ERROR_NONE) {
176 sfg->Accel.iWhoAmI = reg;
178 case MMA8451_WHO_AM_I_WHOAMI_VALUE:
179 case MMA8452_WHO_AM_I_WHOAMI_VALUE:
180 case MMA8453_WHO_AM_I_WHOAMI_VALUE:
break;
181 default:
return(SENSOR_ERROR_INIT);
189 if (reg==MMA8451_WHO_AM_I_WHOAMI_VALUE) {
190 status = Sensor_I2C_Write(sensor->
bus_driver, sensor->
addr, MMA8451_Initialization );
192 status = Sensor_I2C_Write(sensor->
bus_driver, sensor->
addr, MMA845x_Initialization );
201 sfg->Accel.isEnabled =
true;
211 uint8_t sensor_fifo_count = 1;
216 return SENSOR_ERROR_INIT;
220 status = Sensor_I2C_Read(sensor->
bus_driver, sensor->
addr, MMA845x_F_STATUS_READ, I2C_Buffer );
221 if (status==SENSOR_ERROR_NONE) {
222 if (sfg->Accel.iWhoAmI==MMA8451_WHO_AM_I_WHOAMI_VALUE) {
223 sensor_fifo_count = I2C_Buffer[0] & 0x3F;
225 sensor_fifo_count = (I2C_Buffer[0] & 0x08)>>3;
229 if (sensor_fifo_count == 0)
return SENSOR_ERROR_READ;
234 MMA845x_DATA_READ[0].readFrom = MMA845x_OUT_X_MSB;
235 MMA845x_DATA_READ[0].numBytes = 6 * sensor_fifo_count;
236 status = Sensor_I2C_Read(sensor->
bus_driver, sensor->
addr, MMA845x_DATA_READ, I2C_Buffer );
238 if (status==SENSOR_ERROR_NONE) {
239 for (j = 0; j < sensor_fifo_count; j++)
241 sample[
CHX] = (I2C_Buffer[6 * j ] << 8) | I2C_Buffer[6 * j + 1];
242 sample[
CHY] = (I2C_Buffer[6 * j + 2] << 8) | I2C_Buffer[6 * j + 3];
243 sample[
CHZ] = (I2C_Buffer[6 * j + 4] << 8) | I2C_Buffer[6 * j + 5];
254 const registerwritelist_t MMA845x_IDLE[] =
257 { MMA845x_CTRL_REG1, 0x00, 0x01 },
266 status = Sensor_I2C_Write(sensor->
bus_driver, sensor->
addr, MMA845x_IDLE );
268 sfg->Accel.isEnabled =
false;
270 return SENSOR_ERROR_INIT;
274 #endif // F_USING_ACCEL #define CHY
Used to access Y-channel entries in various data data structures.
#define MMA845x_COUNTSPERG
#define MMA8451_ACCEL_FIFO_SIZE
void addToFifo(FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
addToFifo is called from within sensor driver read functions
The top level fusion structure.
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
#define ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures...
The sensor_fusion.h file implements the top level programming interface.
Provides function prototypes for driver level interfaces.
#define CHX
Used to access X-channel entries in various data data structures.
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two's complement value for a 16-...
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
#define F_USING_ACCEL
nominally 0x0001 if an accelerometer is to be used, 0x0000 otherwise
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
uint16_t addr
I2C address if applicable.